package www.wen.com.common.lib.view.popuview

import android.content.Context
import android.text.InputFilter
import android.text.method.ReplacementTransformationMethod
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.core.widget.addTextChangedListener
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.util.XPopupUtils
import www.wen.com.common.R
import www.wen.com.common.lib.extension.setOnClickListenerAgency
import www.wen.com.common.lib.extension.trimProxy
import www.wen.com.common.lib.helper.ResultUiCallBack
import www.wen.com.common.lib.util.KeyboardUtils

/**
 * @author WEN
 * @Description:输入用户信息的弹框
 * @date 2021/3/3 14:53
 */
class InputContextPopupView(
    context: Context,
    val title: CharSequence,
    val content: CharSequence?,
    private val transformationMethod: ReplacementTransformationMethod? = null,
    private val confirmContent: CharSequence,
    private val hintStr: CharSequence,
    private val inputFilters: Array<InputFilter>,
    private val finishOnCancel: Boolean = true,
    private val resultCallBack: ResultUiCallBack<String>? = null,
    private val inputEditorInfo: Int? = EditorInfo.TYPE_CLASS_NUMBER,
) : CenterPopupView(context), View.OnClickListener {

    private lateinit var cancelView: View

    private lateinit var enterView: TextView

    private lateinit var etContent: EditText

    private lateinit var tvTitle: TextView

    private lateinit var ivClear: ImageView

    override fun getImplLayoutId(): Int = R.layout.popupview_input_layout

    override fun onClick(v: View) {
        when (v.id) {
            R.id.cancel -> {
                if (finishOnCancel) {
                    dismiss()
                }
            }
            R.id.confirm -> {
                if (etContent.text.isBlank()) {
                    dismiss()
                } else {
                    resultCallBack?.invoke(v, etContent.trimProxy(), this)
                }
            }
        }
    }

    override fun initPopupContent() {
        super.initPopupContent()

        cancelView = findViewById(R.id.cancel)
        enterView = findViewById(R.id.confirm)

        etContent = findViewById(R.id.tvContent)
        tvTitle = findViewById(R.id.tvTitle)
        ivClear = findViewById(R.id.ivClear)

        etContent.filters = inputFilters
        etContent.hint = hintStr

        etContent.inputType = inputEditorInfo!!

        tvTitle.text = title

        enterView.text = confirmContent

        if (!content.isNullOrBlank()) {
            etContent.setText(content)
            etContent.setSelection(etContent.text.toString().length)
        }

        if (transformationMethod != null) {
            etContent.transformationMethod = transformationMethod
        }

        etContent.addTextChangedListener {
            ivClear.isVisible = !it.isNullOrEmpty()
        }
        ivClear.setOnClickListener {
            etContent.setText("")
        }

        setOnClickListenerAgency(this, cancelView, enterView)
    }

    override fun dismiss() {
        super.dismiss()
        KeyboardUtils.hideSoftInput(this)
    }

    override fun getMaxWidth(): Int {
        return (XPopupUtils.getScreenWidth(context) * 0.8f).toInt()
    }


}


